<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class company extends CI_Controller {
        public $mydata = array();
 
        function __construct()
        {
            parent::__construct();

            $this->load->model('companymodel');
            
            $per_page = 10;
            
            $total_rows = $this->companymodel->record_count();
            $total_links = $total_rows % $per_page;
            $cur_page = 1;
            $sort_by = 'id';
            $asds = 'asc';
            $terms = '';
            if (isset($_GET['page'])){
                $cur_page = $_GET['page'];
            }else{
                $cur_page = 1;
            }
            if (isset($_GET['by'])){
                $sort_by = $_GET['by'];
            }else{
                $sort_by = 'id';
            }
            if (isset($_GET['sort'])){
                $asds = $_GET['sort'];
            }else{
                $asds = 'asc';
            }
            if (isset($_GET['search'])){
                $terms = $_GET['search'];
                $cur_page = 1;
                $offset = ($cur_page - 1) * $per_page;
                $total_rows = $this->companymodel->record_count($per_page, $offset, $sort_by, $asds, $terms);
                $total_links = $total_rows % $per_page;
            }else{
                $terms = '';
            }
            $offset = ($cur_page - 1) * $per_page;
            $this->mydata['links'] = $total_links;
            $this->mydata['cur_page'] = $cur_page;
            $this->mydata['by'] = $sort_by;
            $this->mydata['order'] = $asds;
            $this->mydata['total_row'] = $total_rows;
            $this->mydata['results'] = $this->companymodel->fetch_companies($per_page, $offset, $sort_by, $asds, $terms);
        }

        
	public function index()
	{ 
            $this->load->model('adminmodel');
            $data['query'] = $this->adminmodel->getAllRole();
            $data['active'] = 'Company';
            $this->load->view('header',$data);
            $this->load->view('companyView',$this->mydata);



	}
        function toJsonAll()
        {
            echo json_encode($this->mydata['query_c']); 
            
        }
        function toJsonByID()
        {
            
        }

}